<?php
namespace app\api\controller;
use think\facade\Db;

class Product extends common
{
    private $dbname = 'product';
    public function index()
    {
        return 'product';
    }

    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {
        $data = input('get.');

        $where = [];
        if(!empty($data['name'])){
            $where[] = ['product.name
                |product.content
                ','like','%'.$data['name'].'%'];
        }

        if(!empty($data['status'])){
            $where[] = ['product.status','=',$data['status']];
        }else{
            $where[] = ['product.status','in',[0,1,2]];
        }

        $list = Db::view($this->dbname,'id,image,name,status,time,company_id,product_type_id')
            ->where($where)
            ->view('company', 'name as company_name', 'company.id = product.company_id', 'LEFT')

            ->whereOr('company.name','like','%'.$data['name'].'%')
            ->view('product_type', 'name as product_type_name', 'product_type.id = product.product_type_id', 'LEFT')
            ->whereOr('product_type.name','like','%'.$data['name'].'%')

            ->order('status desc,id desc')
            ->paginate([
                'list_rows'=> $data['limit'],
                'var_page' => 'page',
            ])->toArray();
        foreach ($list['data'] as $k=>$v){
            $img = $v['image']?json_decode($v['image'],true):null;

            $list['data'][$k]['image'] = $img && [0]?$img[0]['url']:null;
        }
        return re_list($list);
    }

    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {


        $data = input('post.');
        $data['time']=ThisTime();
        $model = new \app\api\model\Product();
        $data['image'] = $data['image']?json_encode($data['image']):[];
        $data['user_id'] = $this->getThisUser()['uid'];
        $data['images'] = $data['images']?json_encode($data['images']):[];
        $data['protection_image'] = $data['protection_image']?json_encode($data['protection_image']):[];
        if( 2147483647<strlen($data['content'])){
            return re_warning('您的详情内容过大，请注意您详情中的图片文件。');
        }
        $er = $model->add_vali($data);
        if($er){
            return  $er;
        }
        //模型写入
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        $data['time']=ThisTime();

        if( 2147483647<strlen($data['content'])){
            return re_warning('您的详情内容过大，请注意您详情中的图片文件。');
        }
        $data['image'] = $data['image']?json_encode($data['image']):[];
        $data['images'] = $data['images']?json_encode($data['images']):[];
        $data['protection_image'] = $data['protection_image']?json_encode($data['protection_image']):[];
        //模型验证
        $model = new \app\api\model\Product();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        $data['user_id'] = $this->getThisUser()['uid'];

        return $model->edit($data);

    }

    /**
     * 修改
     * @return false|string|void
     */
    public function updateStatus()
    {
        $data = input('put.');
        //模型验证
        $model = new \app\api\model\Product();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        return $model->editStatus($data);

    }


    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {

        $info = Db::view($this->dbname, '*')
            ->view('company', 'name as company_name', 'company.id = product.company_id', 'LEFT')
            ->view('product_type', 'name as product_type_name', 'product_type.id = product.product_type_id', 'LEFT')
            ->find(input('get.id'));
        if($info){
            $info['images'] = $info['images']?json_decode($info['images']):[];
            $info['image'] = $info['image']?json_decode($info['image']):[];
            $info['protection_image'] = $info['protection_image']?json_decode($info['protection_image']):[];
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }

    public function token(){
        return re_success('获取成功',input('get.'));

    }
    /**
     * 删除
     * @return false|string|void
     */
    public function dele()
    {
        $data = input('delete.');
        $model = new \app\api\model\Product();
        $er = $model->dele_vali($data);
        if($er){
            return  $er;
        }

        return $model->dele($data['id']);
    }


    /**
     * 根据单位查询所有产品。
     * @return string
     */
    public function getSeleList()
    {
        $data = input('get.');
        $where[] = ['company_id','=',$data['company_id']];
        $where[] = ['status','=',1];
        $list = Db::name($this->dbname)->where($where)->field('id,name,status')->select()->toArray();
        return re_success('获取成功',$list);
    }

}
